using System;
using System.IO;
using log4net;
using log4net.Config;
using log4net.Repository;

namespace twistmachinev2.CommonUse
{
    /// <summary>
    /// Log4Net日志管理类
    /// 提供统一的日志记录接口，支持每日生成新的日志文件
    /// </summary>
    public static class LogManager
    {
        private static readonly ILog logger;
        private static readonly ILoggerRepository repository;

        static LogManager()
        {
            // 确保日志目录存在
            EnsureLogDirectoryExists();
            
            // 使用默认日志仓库（由Program.cs中的XmlConfigurator.Configure配置）
            repository = log4net.LogManager.GetRepository();
            
            // 获取日志记录器
            logger = log4net.LogManager.GetLogger(typeof(LogManager));
        }

        /// <summary>
        /// 确保日志目录存在
        /// </summary>
        private static void EnsureLogDirectoryExists()
        {
            string logDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs");
            if (!Directory.Exists(logDirectory))
            {
                Directory.CreateDirectory(logDirectory);
            }
        }

        /// <summary>
        /// 记录信息日志
        /// </summary>
        /// <param name="message">日志消息</param>
        public static void LogInfo(string message)
        {
            logger.Info(message);
        }

        /// <summary>
        /// 记录信息日志
        /// </summary>
        /// <param name="message">日志消息</param>
        /// <param name="args">格式化参数</param>
        public static void LogInfo(string message, params object[] args)
        {
            logger.InfoFormat(message, args);
        }

        /// <summary>
        /// 记录警告日志
        /// </summary>
        /// <param name="message">日志消息</param>
        public static void LogWarning(string message)
        {
            logger.Warn(message);
        }

        /// <summary>
        /// 记录警告日志
        /// </summary>
        /// <param name="message">日志消息</param>
        /// <param name="args">格式化参数</param>
        public static void LogWarning(string message, params object[] args)
        {
            logger.WarnFormat(message, args);
        }

        /// <summary>
        /// 记录错误日志
        /// </summary>
        /// <param name="message">日志消息</param>
        public static void LogError(string message)
        {
            logger.Error(message);
        }

        /// <summary>
        /// 记录错误日志
        /// </summary>
        /// <param name="message">日志消息</param>
        /// <param name="exception">异常对象</param>
        public static void LogError(string message, Exception exception)
        {
            logger.Error(message, exception);
        }

        /// <summary>
        /// 记录错误日志
        /// </summary>
        /// <param name="exception">异常对象</param>
        public static void LogError(Exception exception)
        {
            logger.Error(exception.Message, exception);
        }

        /// <summary>
        /// 记录错误日志
        /// </summary>
        /// <param name="message">日志消息</param>
        /// <param name="args">格式化参数</param>
        public static void LogError(string message, params object[] args)
        {
            logger.ErrorFormat(message, args);
        }

        /// <summary>
        /// 记录调试日志
        /// </summary>
        /// <param name="message">日志消息</param>
        public static void LogDebug(string message)
        {
            logger.Debug(message);
        }

        /// <summary>
        /// 记录调试日志
        /// </summary>
        /// <param name="message">日志消息</param>
        /// <param name="args">格式化参数</param>
        public static void LogDebug(string message, params object[] args)
        {
            logger.DebugFormat(message, args);
        }

        /// <summary>
        /// 记录OPC UA数据变更日志
        /// </summary>
        /// <param name="nodeId">节点ID</param>
        /// <param name="oldValue">旧值</param>
        /// <param name="newValue">新值</param>
        /// <param name="userName">操作用户</param>
        public static void LogOpcUaDataChange(string nodeId, object oldValue, object newValue, string userName = "System")
        {
            string message = $"OPC UA数据变更 - 节点: {nodeId}, 旧值: {oldValue}, 新值: {newValue}, 操作用户: {userName}";
            LogInfo(message);
        }

        /// <summary>
        /// 记录OPC UA连接状态变更日志
        /// </summary>
        /// <param name="serverUrl">服务器地址</param>
        /// <param name="status">连接状态</param>
        public static void LogOpcUaConnectionStatus(string serverUrl, string status)
        {
            string message = $"OPC UA连接状态变更 - 服务器: {serverUrl}, 状态: {status}";
            LogInfo(message);
        }

        /// <summary>
        /// 记录系统操作日志
        /// </summary>
        /// <param name="operation">操作类型</param>
        /// <param name="details">操作详情</param>
        /// <param name="userName">操作用户</param>
        public static void LogSystemOperation(string operation, string details, string userName = "System")
        {
            string message = $"系统操作 - 操作: {operation}, 详情: {details}, 用户: {userName}";
            LogInfo(message);
        }
    }
}